package com.kmxd.ams.app.use;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmxd.ams.client.use.IUseBorrowLogSvc;
import com.kmxd.ams.client.use.dto.UseBorrowLogAddDTO;
import com.kmxd.ams.client.use.dto.UseBorrowLogEditDTO;
import com.kmxd.ams.client.use.dto.UseBorrowLogQueryDTO;
import com.kmxd.ams.client.use.vo.UseBorrowLogListVO;
import com.kmxd.ams.client.use.vo.UseBorrowLogVO;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.infra.use.entity.UseBorrowLog;
import com.kmxd.ams.infra.use.mapper.UseBorrowLogMapper;
import java.util.List;
import java.util.Set;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 借阅日志表 服务实现类
 *
 * @author ZuoLG
 * @since 2022-08-02
 */
@Service
@AllArgsConstructor
public class UseBorrowLogSvcImpl extends ServiceImpl<UseBorrowLogMapper, UseBorrowLog>
    implements IUseBorrowLogSvc {

  private UseBorrowLogMapper useBorrowLogMapper;

  @Override
  public Page<UseBorrowLogListVO> selectPage(Page reqPage, UseBorrowLogQueryDTO req) {
    LambdaQueryWrapper<UseBorrowLog> queryWrapper =
        QueryGen.init(new QueryWrapper<UseBorrowLog>(), req).lambda();
    Page<UseBorrowLog> page = useBorrowLogMapper.selectPage(reqPage, queryWrapper);
    Page<UseBorrowLogListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), UseBorrowLogListVO.class));
    return voPage;
  }

  @Override
  public List<UseBorrowLogListVO> selectList(UseBorrowLogQueryDTO req) {
    LambdaQueryWrapper<UseBorrowLog> queryWrapper =
        QueryGen.init(new QueryWrapper<UseBorrowLog>(), req).lambda();
    List<UseBorrowLog> list = useBorrowLogMapper.selectList(queryWrapper);
    return BeanUtil.copyToList(list, UseBorrowLogListVO.class);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(UseBorrowLogAddDTO req) {
    UseBorrowLog entity = BeanUtil.copyProperties(req, UseBorrowLog.class);
    useBorrowLogMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(UseBorrowLogEditDTO req) {
    UseBorrowLog entity = BeanUtil.copyProperties(req, UseBorrowLog.class);
    useBorrowLogMapper.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    this.baseMapper.deleteBatchIds(idList);
  }

  @Override
  public UseBorrowLogVO view(Long id) {
    UseBorrowLog entity = useBorrowLogMapper.selectById(id);
    return BeanUtil.copyProperties(entity, UseBorrowLogVO.class);
  }
}
